import axios from 'axios';

const API_KEY = '0dD1MtYYdjrppjSyLzLg00tP';
const SECRET_KEY = 'drApGhGxOFrmj3VzgLe72ngloHLoQ6A9';
const ACCESS_TOKEN_URL = 'https://aip.baidubce.com/oauth/2.0/token';

// 获取 access token
async function fetchAccessToken() {
    const accessTokenRes = await axios.post(ACCESS_TOKEN_URL, null, {
        params: {
            'grant_type': 'client_credentials',
            'client_id': API_KEY,
            'client_secret': SECRET_KEY,
        },
    });
    return accessTokenRes.data.access_token;
}

const CHAT_URL = 'https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/yi_34b_chat';

export async function ask(question) {
    const messages = [{ role: 'user', content: question }];
    const token = await getAccessToken();
    const res = await axios.post(
        CHAT_URL,
        { messages },
        { params: { 'access_token': token } }
    );
    const { data } = res;
    return data;
}

// async function main() {
//     const question = process.argv[2];
//     if (!question) {
//         console.log('Usage: node main.mjs <question>');
//         return;
//     }
//     const res = await ask(question);
//     console.log(res);
// }

// main();
